package com.example.redis.service;

import com.example.redis.domain.User;
import com.example.redis.utils.RedisCache;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.concurrent.TimeUnit;

@Service
public class RedisService {

    @Autowired
    private RedisCache redisCache;

    public void string(){
        User user = new User("mike","123456");
        redisCache.setCacheObject("user",user);
        User redisUser = redisCache.getCacheObject("user");
        System.out.println(redisUser);
    }

    public void list(){
        List<String> list = new ArrayList<>();
        list.add("aaa");
        list.add("bbb");
        redisCache.setCacheList("list",list);
        List<Object> redisList = redisCache.getCacheList("list");
        System.out.println(redisList);
    }

    public void set(){
        Set<String> set = new HashSet<>();
        set.add("aaa");
        set.add("bbb");
        redisCache.setCacheSet("set",set);
        Set<String> redisSet = redisCache.getCacheSet("set");
        System.out.println(redisSet);
    }

    public void map(){
        User user = new User("mike","123456");
        Map<String,String> map = new HashMap<>(16);
        map.put("username", user.getUsername());
        map.put("password", user.getPassword());
        redisCache.setCacheMap("map",map);
        Map<String,String> redisMap = redisCache.getCacheMap("map");
        System.out.println(redisMap);
    }

    public void ZSet(){
        String key = "ZSet_key";
        User user1 = new User("Tom", "123");
        User user2 = new User("Jack", "123");
        Set<User> hashSet = new HashSet<>();
        hashSet.add(user1);
        hashSet.add(user2);
        redisCache.zSet(key,user1,0);
        redisCache.zSet(key,user2,1);
        Set set = redisCache.zGet(key, 0, -1);
        System.out.println(set);
    }
}
